package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.common.utils.bean.BeanUtils;
import com.nt.oam.domain.*;
import com.nt.oam.vo.asset.SoftwareAssetVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntSoftwareAssetMapper;
import com.nt.oam.service.IAntSoftwareAssetService;

/**
 * 软件资产Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntSoftwareAssetServiceImpl extends ServiceImpl<AntSoftwareAssetMapper, AntSoftwareAsset> implements IAntSoftwareAssetService {
    private final AntSoftwareAssetMapper antSoftwareAssetMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final AntHospitalServiceImpl antHospitalService;
    private final AntHospitalProjectServiceImpl antHospitalProjectService;
    private final AntAssetCorrelationServiceImpl antAssetCorrelationService;
    private final SysUserServiceImpl sysUserService;

    /**
     * 查询软件资产
     *
     * @param softwareAssetId 软件资产主键
     * @return 软件资产
     */
    @Override
    public AntSoftwareAsset selectAntSoftwareAssetBySoftwareAssetId(Long softwareAssetId) {
        return antSoftwareAssetMapper.selectAntSoftwareAssetBySoftwareAssetId(softwareAssetId);
    }

    /**
     * 查询软件资产列表
     *
     * @param antSoftwareAsset 软件资产
     * @return 软件资产
     */
    @Override
    public List<AntSoftwareAsset> selectAntSoftwareAssetList(AntSoftwareAsset antSoftwareAsset) {
        return antSoftwareAssetMapper.selectAntSoftwareAssetList(antSoftwareAsset);
    }

    /**
     * 新增软件资产
     *
     * @param antSoftwareAsset 软件资产
     * @return 结果
     */
    @Override
    public int insertAntSoftwareAsset(AntSoftwareAsset antSoftwareAsset) {
        antSoftwareAsset.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antSoftwareAsset.getHospitalId();
        antSoftwareAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antSoftwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antSoftwareAsset.getProjectId();
        antSoftwareAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antSoftwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
           antSoftwareAsset.setCreatePerson(sysUser.getUserName());
        }
        antSoftwareAsset.setIsDeleted(YesOrNoEnum.NO.getCode());
        antSoftwareAsset.setIsModify(YesOrNoEnum.NO.getCode());
        return antSoftwareAssetMapper.insertAntSoftwareAsset(antSoftwareAsset);
    }

    /**
     * 修改软件资产
     *
     * @param antSoftwareAsset 软件资产
     * @return 结果
     */
    @Override
    public int updateAntSoftwareAsset(AntSoftwareAsset antSoftwareAsset) {
        antSoftwareAsset.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antSoftwareAsset.getHospitalId();
        antSoftwareAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antSoftwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antSoftwareAsset.getProjectId();
        antSoftwareAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antSoftwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 更新人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
           antSoftwareAsset.setUpdatePerson(sysUser.getUserName());
        }
        return antSoftwareAssetMapper.updateAntSoftwareAsset(antSoftwareAsset);
    }

    /**
     * 批量删除软件资产
     *
     * @param softwareAssetIds 需要删除的软件资产主键
     * @return 结果
     */
    @Override
    public int deleteAntSoftwareAssetBySoftwareAssetIds(Long[] softwareAssetIds) {
        List<Long> softwareAssetIdsList = Arrays.asList(softwareAssetIds);
        softwareAssetIdsList.forEach(a -> {
            AntSoftwareAsset antSoftwareAsset = antSoftwareAssetMapper.selectAntSoftwareAssetBySoftwareAssetId(a);
            antSoftwareAsset.setIsDeleted(YesOrNoEnum.YES.getCode());
            antSoftwareAssetMapper.updateAntSoftwareAsset(antSoftwareAsset);
        });
        // antSoftwareAssetMapper.deleteAntSoftwareAssetBySoftwareAssetIds(softwareAssetIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除软件资产信息
     *
     * @param softwareAssetId 软件资产主键
     * @return 结果
     */
    @Override
    public int deleteAntSoftwareAssetBySoftwareAssetId(Long softwareAssetId) {
        return antSoftwareAssetMapper.deleteAntSoftwareAssetBySoftwareAssetId(softwareAssetId);
    }


    /**
     * 查询软件资产详情信息
     *
     * @param softwareAssetId 软件资产主键
     * @return 软件资产
     */
    @Override
    public SoftwareAssetVo selectSoftwareAsset(Long softwareAssetId) {
        // 源对象
        AntSoftwareAsset antSoftwareAsset = antSoftwareAssetMapper.selectAntSoftwareAssetBySoftwareAssetId(softwareAssetId);
        //医院名称
        AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antSoftwareAsset.getHospitalId());
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antSoftwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目名称
        AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antSoftwareAsset.getProjectId());
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antSoftwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 资产名称
        AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antSoftwareAsset.getAssetId());
        if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
            antSoftwareAsset.setAssetName(antAssetCorrelation.getAssetName());
        }
        // 目标对象
        SoftwareAssetVo softwareAssetVo = new SoftwareAssetVo();
        // 对象互换
        BeanUtils.copyProperties(antSoftwareAsset, softwareAssetVo);
        return softwareAssetVo;
    }

    /**
     * 查询软件资产详情信息-----接口
     *
     * @return 软件资产
     */
    @Override
    public List<SoftwareAssetVo> selectByProjectIdSoftwareAsset(Long projectId) {
        List<SoftwareAssetVo> softwareAssetVoList = new ArrayList<>();
        // 源对象
        List<AntSoftwareAsset> antSoftwareAssetList = antSoftwareAssetMapper.selectByProjectIdSoftwareAsset(projectId);
        if (Objects.nonNull(antSoftwareAssetList) && antSoftwareAssetList.size() > 0) {
            antSoftwareAssetList.forEach(antSoftwareAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antSoftwareAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antSoftwareAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antSoftwareAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antSoftwareAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 目标对象
                SoftwareAssetVo softwareAssetVo = new SoftwareAssetVo();
                // 对象互换
                BeanUtils.copyProperties(antSoftwareAsset, softwareAssetVo);
                softwareAssetVoList.add(softwareAssetVo);
            });
        }
        return softwareAssetVoList;
    }
    /**
     * 查询软件资产详情信息-----接口
     *
     * @return 软件资产
     */
    @Override
    public List<SoftwareAssetVo> selectByHospitalIdSoftwareAsset(Long hospitalId) {
        List<SoftwareAssetVo> softwareAssetVoList = new ArrayList<>();
        // 源对象
        List<AntSoftwareAsset> antSoftwareAssetList = antSoftwareAssetMapper.selectByHospitalIdSoftwareAsset(hospitalId);
        if (Objects.nonNull(antSoftwareAssetList) && antSoftwareAssetList.size() > 0) {
            antSoftwareAssetList.forEach(antSoftwareAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antSoftwareAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antSoftwareAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antSoftwareAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antSoftwareAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 目标对象
                SoftwareAssetVo softwareAssetVo = new SoftwareAssetVo();
                // 对象互换
                BeanUtils.copyProperties(antSoftwareAsset, softwareAssetVo);
                softwareAssetVoList.add(softwareAssetVo);
            });
        }
        return softwareAssetVoList;
    }


    /**
     * 查询软件资产详情信息-----系统
     *
     * @return 软件资产
     */
    @Override
    public SoftwareAssetVo selectByProjectIdSoftwareAssetSystem(Long projectId) {
        // 源对象
        AntSoftwareAsset antSoftwareAsset = antSoftwareAssetMapper.selectByProjectIdSoftwareAssetSystem(projectId);
        //医院名称
        AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antSoftwareAsset.getHospitalId());
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antSoftwareAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目名称
        AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antSoftwareAsset.getProjectId());
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antSoftwareAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 资产名称
        AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antSoftwareAsset.getAssetId());
        if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
            antSoftwareAsset.setAssetName(antAssetCorrelation.getAssetName());
        }
        // 目标对象
        SoftwareAssetVo softwareAssetVo = new SoftwareAssetVo();
        // 对象互换
        BeanUtils.copyProperties(antSoftwareAsset, softwareAssetVo);
        return softwareAssetVo;
    }


    /**
     * 查询是否在修改
     */
    public int selectSoftwareModify(Long softwareAssetId) {
        AntSoftwareAsset antSoftwareAsset = this.getById(softwareAssetId);
        int isModify = 0;
        if (Objects.nonNull(antSoftwareAsset) && Objects.nonNull(antSoftwareAsset.getIsModify())) {
            isModify = antSoftwareAsset.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateSoftwareModify(Long softwareAssetId, Integer isModify) {
        AntSoftwareAsset antSoftwareAsset = this.getById(softwareAssetId);
        if (Objects.nonNull(antSoftwareAsset)) {
            antSoftwareAsset.setIsModify(isModify);
        }
        return antSoftwareAssetMapper.updateAntSoftwareAsset(antSoftwareAsset);
    }
}
